<?php


namespace app\api\controller;
use app\api\model\Base as bs;
use think\Db;
use think\Request;

class Mycourse extends Base
{
    public function mycourse(){
        //这个根据业务场景，一个人选择的课程不多，以及我觉得这样查，数据库压力大，所以我用的前端分页
        if(Request::instance()->method()== 'GET'){
            $id = I('id');
//            $page = input('current','1','intval');
//            $page_size = input('pageSize',10,'intval');
            $data = Db::name('course')
                ->alias('a')
                ->join('course_department b','a.id = b.course_id','INNER')
                ->join('department c','b.department_id = c.id','INNER')
                ->join('course_classroom d','a.id = d.course_id','INNER')
                ->join('classroom e','d.classroom_id = e.id','INNER')
                ->join('user_course f','f.uid = '.$id,'INNER')
                ->join('user g','a.teacher_id = g.id','INNER')
                ->field(['e.id'=>'classroom_id','e.name'=>'classroom_name'])
                ->field('a.course_name,a.people_num,a.time,c.department_name')
                ->field(['a.id'=>'key','c.id'=>'department_id'])
                ->field(['g.username'=>'teacher_name'])
                ->where('a.id = f.course_id')
                ->where('f.uid',$id)
                ->where('f.status',1)
//                ->page($page,$page_size)
                ->Distinct(true)
                ->select();
//            $total = bs::total('course','');
            $this->apiReturn(200,'',$data,count($data));
        }

        if(Request::instance()->method()== 'POST'){
            $value = I('id|course_id');
            foreach ($value as $k=>$v){
                if(!$v){
                    $this->apiReturn(303,'lack '.$k);
                }
            }
            Db::startTrans();
            try{
                $user_course = Db::name('user_course')->insertGetId([
                    'uid'=>$value['id'],
                    'course_id'=>$value['course_id'],
                ]);
                $course = Db::table('course')->where('id', $value['course_id'])
                    ->setDec('people_num');
                $people_num = bs::selectId('course','people_num',$value['course_id']);
                //people_num查出来是一个对象
                if($people_num['people_num'] < 0 ){
                    Db::rollback();
                    $this->apiReturn(200,'课程已经被抢完了',['id'=>$user_course]);
                }
                if($user_course && $course){
                    Db::commit();
                    $this->apiReturn(200,'',['id'=>$user_course]);
                    return true;
                }
            }catch (Exception $e){
                Db::rollback();
                return $e->getMessage();
            }
        }
        if(Request::instance()->method()== 'PUT'){
            //因为课程涉及抢课，没有修改，所以不弄
        }
        if(Request::instance()->method()== 'DELETE'){
            $id = input('id');
            $course_id = input('course_id');
            if(!$id || !$course_id){
                $this->apiReturn(303,'lack ');
            }
            Db::name('user_course')->where([
                'uid'=>$id,
                'course_id'=>$course_id
            ])->update(['status'=>config('status')]);
            //课程人数+1
            $course = Db::table('course')->where('id', $course_id)
                ->setInc('people_num');
            $this->apiReturn(200,'',$id);
        }
    }

}